package com.servlet;

import com.bdqn.demo.pojo.User;
import com.bdqn.demo.service.UserService;
import com.bdqn.demo.service.impl.UserServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "servlet",value = "/aa")
public class insert  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if((username==null||username.equals(""))||(password==null||password.equals(""))){
            req.setAttribute("a","用户名或密码不能为空");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }else {
            UserService userService=new UserServiceimpl();
            User user=new User();
            user.setUsername(username);
            user.setPassword(password);
            int b = userService.selectCount(username);
            if (b==0||"".equals(b)){
                int a = userService.insert(user);
                if(a>0){
                req.setAttribute("a","注册成功!");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
                req.setAttribute("a","注册失败");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }else {
                req.setAttribute("a","注册失败");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
